const calculator = require('../utils/calculator');

/**
 * 理财产品信息模型
 */
class Financial {
  constructor(data = {}) {
    this.id = data._id || '';
    this.bank = data.bank || '';
    this.type = data.type || '';
    this.name = data.name || '';
    this.money = data.money || 0;
    this.profit = data.profit || 0;
    this.arr = data.arr || 0;
    this.buyDate = data.buyDate || '';
    this.endDate = data.endDate || '';
    this.remark = data.remark || '';
    this.createTime = data.create_time;
    this.status = data.status || 1;
    this.isDeleted = data.is_deleted || 0;
  }

  // 计算持有天数
  getHoldingDays() {
    return calculator.calculateHoldingDays(this.buyDate, this.endDate);
  }

  // 计算年化收益率
  calculateAnnualizedReturn() {
    const days = this.getHoldingDays();
    return calculator.calculateAnnualizedReturn(this.money, this.money + this.profit, days);
  }

  // 获取当前市值
  getCurrentValue() {
    return this.money + this.profit;
  }

  // 转换为API提交格式（关键修改：buy_date -> buyDate；end_date -> endDate）
  toApiFormat() {
    return {
      id: this.id,
      bank_id: this.bankId,
      type: this.type,
      name: this.name,
      money: this.money,
      profit: this.profit,
      arr: this.arr,
      buyDate: this.buyDate,  // 原 buy_date 改为 buyDate
      endDate: this.endDate,  // 原 end_date 改为 endDate
      remark: this.remark,
      status: this.status,
      is_deleted: this.isDeleted
    };
  }
}

/**
 * 理财产品操作历史模型
 */
class FinancialOperation {
  constructor(data = {}) {
    this.id = data.id || '';
    this.financialId = data.financial_id || '';
    this.type = data.type || '';
    this.money = data.money || 0;
    this.submitDate = data.submit_date || '';
    this.createUser = data.create_user;
    this.createDept = data.create_dept;
    this.createTime = data.create_time;
    this.updateUser = data.update_user;
    this.updateTime = data.update_time;
    this.status = data.status || 1;
    this.isDeleted = data.is_deleted || 0;
  }

  // 转换为API提交格式
  toApiFormat() {
    return {
      id: this.id,
      financial_id: this.financialId,
      type: this.type,
      money: this.money,
      submit_date: this.submitDate,
      status: this.status,
      is_deleted: this.isDeleted
    };
  }
}

/**
 * 理财产品盈利历史模型
 */
class FinancialProfit {
  constructor(data = {}) {
    this.id = data.id || '';
    this.financialId = data.financial_id || '';
    this.profit = data.profit || 0;
    this.arr = data.arr || 0;
    this.submitDate = data.submit_date || '';
    this.createUser = data.create_user;
    this.createDept = data.create_dept;
    this.createTime = data.create_time;
    this.updateUser = data.update_user;
    this.updateTime = data.update_time;
    this.status = data.status || 1;
    this.isDeleted = data.is_deleted || 0;
  }

  // 转换为API提交格式
  toApiFormat() {
    return {
      id: this.id,
      financial_id: this.financialId,
      profit: this.profit,
      arr: this.arr,
      submit_date: this.submitDate,
      status: this.status,
      is_deleted: this.isDeleted
    };
  }
}

/**
 * 理财产品年度盈利分析模型
 */
class FinancialProfitYear {
  constructor(data = {}) {
    this.id = data.id || '';
    this.name = data.name || '';
    this.financialId = data.financial_id || '';
    this.profit = data.profit || 0;
    this.arr = data.arr || 0;
    this.year = data.year || new Date().getFullYear();
    this.cost = data.cost || 0;
    this.createUser = data.create_user;
    this.createDept = data.create_dept;
    this.createTime = data.create_time;
    this.updateUser = data.update_user;
    this.updateTime = data.update_time;
    this.status = data.status || 1;
    this.isDeleted = data.is_deleted || 0;
  }

  // 转换为API提交格式
  toApiFormat() {
    return {
      id: this.id,
      name: this.name,
      financial_id: this.financialId,
      profit: this.profit,
      arr: this.arr,
      year: this.year,
      cost: this.cost,
      status: this.status,
      is_deleted: this.isDeleted
    };
  }
}

module.exports = {
  Financial,
  FinancialOperation,
  FinancialProfit,
  FinancialProfitYear
};